package cn.sinata.wanglin.neighbor.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.view.SwipeRefreshRecyclerLayout;

import java.util.ArrayList;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.GroupTagsAdapter;
import cn.sinata.wanglin.model.Tag;

public class CreateGroupChatStepTwoActivity extends RecyclerListActivity {

    private GroupTagsAdapter groupTagsAdapter;
    private ArrayList<Tag> mTags = new ArrayList<>();
    private String name;
    private String labels;

    @Override
    protected void initView() {
        super.initView();
        setTitle("选择群标签");
        addRightButton("下一步", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                labels = groupTagsAdapter.getSelectTag();
                if (TextUtils.isEmpty(labels)) {
                    showToast("请选择至少一个标签");
                    return;
                }
                ActivityUtil.create(mContext)
                        .go(CreateGroupChatStepThreeActivity.class)
                        .put("name",name)
                        .put("labels",labels)
                        .startForResult(0);
            }
        });

        name = getIntent().getStringExtra("name");

        setSwipeRefreshLayoutBackground(Color.WHITE);

        defaultTags();

    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(this,3);
    }

    @Override
    protected RecyclerView.ItemDecoration getDefaultItemDecoration() {
        return null;
    }

    @Override
    protected SwipeRefreshRecyclerLayout.Mode getMode() {
        return SwipeRefreshRecyclerLayout.Mode.None;
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        groupTagsAdapter = new GroupTagsAdapter(mTags);
        View headerView = LayoutInflater.from(this).inflate(R.layout.header_create_group_two_layout,null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        headerView.setLayoutParams(params);
        groupTagsAdapter.setHeaderView(headerView);
        View footerView = LayoutInflater.from(this).inflate(R.layout.footer_create_group_two_layout,null);
        footerView.setLayoutParams(params);
        footerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(CreateGroupChatStepTwoActivity.this)
                        .go(AddTagActivity.class)
                        .startForResult(10);
            }
        });
        groupTagsAdapter.setFooterView(footerView);
        return groupTagsAdapter;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0) {
                setResult(RESULT_OK);
                finish();
            } else if (requestCode == 10) {
                if (data != null) {
                    String tag = data.getStringExtra("tag");
                    mTags.add(new Tag(tag));
                    groupTagsAdapter.notifyDataSetChanged();
                }

            }

        }
    }

    void defaultTags(){
        mTags.clear();
        //美食、业主、艺术、运动、亲子、宠物、同乡、娱乐、旅游、商业、教育、其它
        mTags.add(new Tag("美食"));
        mTags.add(new Tag("业主"));
        mTags.add(new Tag("艺术"));
        mTags.add(new Tag("运动"));
        mTags.add(new Tag("亲子"));
        mTags.add(new Tag("宠物"));
        mTags.add(new Tag("同乡"));
        mTags.add(new Tag("娱乐"));
        mTags.add(new Tag("旅游"));
        mTags.add(new Tag("商业"));
        mTags.add(new Tag("教育"));
        mTags.add(new Tag("其它"));
        groupTagsAdapter.notifyDataSetChanged();
    }
}
